package com.example.demo.model.meta;

import com.baidu.bas.annotation.Erupt;
import com.baidu.bas.annotation.EruptField;
import com.baidu.bas.annotation.sub_field.Edit;
import com.baidu.bas.annotation.sub_field.EditType;
import com.baidu.bas.annotation.sub_field.View;
import com.baidu.bas.annotation.sub_field.sub_edit.Search;
import com.baidu.bas.upms.model.base.HyperModel;
import com.deepzero.mmma.api.enums.StorageType;
import com.vladmihalcea.hibernate.type.json.JsonStringType;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;

import javax.persistence.*;
import javax.persistence.Column;
import java.util.HashMap;
import java.util.Map;

/**
 * @author liuzhenchuan@foxmail.com
 * @date 2020/12/22
 */
@Setter
@Getter
@Erupt(name = "物理表",orderBy = "id desc")
@Table(name = "ma_meta_physical_table")
@Entity
@TypeDef(name = "json", typeClass = JsonStringType.class)
@DynamicUpdate
@NoArgsConstructor
public class PhysicalTable extends HyperModel {
    @EruptField(
            views = @View(title = "字段名"),
            edit = @Edit(title = "字段名",type = EditType.INPUT)
    )
    protected String name;
    @EruptField(
            views = @View(title = "字段名"),
            edit = @Edit(title = "字段名",type = EditType.TEXTAREA)
    )
    protected String remarks;

    @ManyToOne(optional = false)
    @JoinColumn(foreignKey = @ForeignKey(name = "fk_logical_table"))
    @EruptField(
            views = @View(title = "逻辑表", column = "name"),
            edit = @Edit(title = "逻辑表", type = EditType.REFERENCE_TABLE, search = @Search)
    )
    protected LogicalTable logicalTable;

    @ManyToOne(optional = false)
    @JoinColumn(name = "logical_catalog_id",foreignKey = @ForeignKey(name = "fk_logical_catalog_2"))
    @EruptField(
            views = @View(title = "逻辑目录", column = "name"),
            edit = @Edit(title = "逻辑目录", type = EditType.REFERENCE_TABLE, search = @Search)
    )
    protected LogicalCatalog logicalCatalog;

    @EruptField(
            views = @View(title = "字段名"),
            edit = @Edit(title = "字段名",type = EditType.BOOLEAN)
    )
    protected Boolean enable = true;

    @EruptField(
            views = @View(title = "字段名"),
            edit = @Edit(title = "字段名",type = EditType.INPUT)
    )
    @Enumerated(value = EnumType.STRING)
    protected StorageType storageType;

    /**
     * 连接参数
     */
    @Type(type = "json")
    @Column(name = "connectParams", columnDefinition = "json")
    @EruptField(
            views = @View(title = "配置"),
            edit = @Edit(title = "配置",type = EditType.HIDDEN)
    )
    protected Map<String,Object> connectParams = new HashMap<>();

    public PhysicalTable(String name) {
        this.name = name;
    }

}
